﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace WmsApp.UI
{
    public interface  ICheckQuerier
    {
        /// <summary>
        /// 启用框的文本内容
        /// </summary>
        string CheckEditOnText { get; set; }

        /// <summary>
        /// 启动框的宽度
        /// </summary>
        int PrevWidth { get; }
        void BindingCheckQuerierItem(List<ICheckQuerierItem> list);
        string GetQueryCondition();
        bool Contains(Control c);
        Control.ControlCollection Controls{ get;}
    }

    public partial class CheckQuerier : DevExpress.XtraEditors.XtraUserControl, ICheckQuerier
    {
        private List<ICheckQuerierItem> checkQuerierItems = null;
        public CheckQuerier()
        {
            InitializeComponent();
        }

        public string CheckEditOnText
        {
            get{ return this.checkEditOn.Properties.Caption;}
            set { this.checkEditOn.Properties.Caption = value; }
        }

        public int PrevWidth
        {
            get { return this.checkEditOn.Width + 3; }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Height = 21;
        }

        public void BindingCheckQuerierItem(List<ICheckQuerierItem> list)
        {
            if (checkQuerierItems != null && checkQuerierItems.Count != 0)
            {
                for (int idx = 0; idx < checkQuerierItems.Count; idx++)
                {
                    checkQuerierItems[idx].Clear();
                }
            }
            checkQuerierItems = list;
            ICheckQuerierItem currentCheckQuerierItem = null;
            for (int idx = 0; idx < list.Count; idx++)
            {
                list[idx].Add(currentCheckQuerierItem);
                currentCheckQuerierItem = list[idx];
            }
        }

        public string GetQueryCondition()
        {
            if(!checkEditOn.Checked)
            {
                return "";
            }
            string prev = "";
            if (checkQuerierItems == null || checkQuerierItems.Count ==0)
            {
                return "";
            }
            else
            {
                for (int idx = 0; idx < checkQuerierItems.Count; idx++)
                {
                    if(checkQuerierItems[idx].Checked)
                    {
                        if(prev == "")
                        {
                            prev = String.Format("'{0}'", checkQuerierItems[idx].Value);
                        }
                        else
                        {
                            prev += ("," + String.Format("'{0}'", checkQuerierItems[idx].Value));
                        }
                    }
                }
                if (prev == "")
                {
                    return "('@#$%&*')";
                }
                return "(" + prev + ")";
            }
        }

        private void CheckEditOnCheckedChanged(object sender, EventArgs e)
        {
            if (checkQuerierItems != null && checkQuerierItems.Count != 0)
            {
                for (int idx = 0; idx < checkQuerierItems.Count; idx++)
                {
                    checkQuerierItems[idx].SetEnable(checkEditOn.Checked);
                }
            }
        }
    }
}
